﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;
namespace DAO
{
    public class ChiTietPhong_SanPhamDAO : Abstract
    {
        public static bool ThemChiTietPhong_SanPham(ChiTietPhong_SanPhamDTO chiTietPhong_SanPham)
        {
            string query = "insert into ChiTietPhong_SanPham ( MaPhieuThuePhong, MaSanPham, SoLuong) values ({1},{2},{3})"
                .Replace("{1}", chiTietPhong_SanPham.MaPhieuThuePhong.ToString())
                .Replace("{2}", chiTietPhong_SanPham.MaSanPham.ToString())
                .Replace("{3}", chiTietPhong_SanPham.SoLuong.ToString());
            return Query(query);
        }

        public static bool SuaChiTietPhong_SanPham(ChiTietPhong_SanPhamDTO chiTietPhong_SanPham)
        {
            string query = "update ChiTietPhong_SanPham set SoLuong = {0} ,TrangThai = {1} where MaCT = {2}"
               .Replace("{0}", chiTietPhong_SanPham.SoLuong.ToString())
               .Replace("{1}", chiTietPhong_SanPham.MaSanPham.ToString())
               .Replace("{2}", chiTietPhong_SanPham.MaCT.ToString());
            return Query(query);
        }

        public static bool XoaChiTietPhong_SanPham(int _maCT)
        {
            string query = "delete from ChiTietPhong_SanPham where MaCT = {0}"
                .Replace("{0}", _maCT.ToString());
            return Query(query);
        }


        public static List<ChiTietPhong_SanPhamDTO> LayDSChiTietPhong_SanPham(int maCT = -1, int maPhieuThuePhong = -1,int maSanPham = -1)
        {
            OleDbConnection conn = null;
            List<ChiTietPhong_SanPhamDTO> listChiTietPhong_SanPham = new List<ChiTietPhong_SanPhamDTO>();
            try
            {
                conn = GetConnection();
                conn.Open();
            }
            catch
            {
                throw new Exception("Connect error!");
            }
            try
            {
                string query = "select MaCT, MaPhieuThuePhong, MaSanPham, SoLuong from ChiTietPhong_SanPham ";
                if (maCT != -1)
                    query = query + "where MaCT = {0}".Replace("{0}", maCT.ToString());
                else if (maPhieuThuePhong != -1)
                    query = query + "where MaPhieuThuePhong = {1}".Replace("{1}", maPhieuThuePhong.ToString());
                else if(maSanPham != -1)
                    query = query + "MaSanPham = {2}".Replace("{2}", maSanPham.ToString()); 
                OleDbDataReader rd = new OleDbCommand(query, conn).ExecuteReader();
                while (rd.Read())
                {
                    ChiTietPhong_SanPhamDTO chiTietPhong_SanPham = new ChiTietPhong_SanPhamDTO();
                    chiTietPhong_SanPham.MaCT = rd.GetInt32(0);
                    chiTietPhong_SanPham.MaPhieuThuePhong = rd.GetInt32(1);
                    chiTietPhong_SanPham.MaSanPham = rd.GetInt32(2);
                    chiTietPhong_SanPham.SoLuong = rd.GetInt32(3);
                 

                    listChiTietPhong_SanPham.Add(chiTietPhong_SanPham);
                }
            }
            catch (Exception e)
            {
                listChiTietPhong_SanPham = null;
            }

            if (conn.State == System.Data.ConnectionState.Open)
                conn.Close();
            return listChiTietPhong_SanPham;
        }
    }
}
